package com.core.zyyz.util;

import com.core.zyyz.amc.modules.domain.BaseDictionary;
import com.core.zyyz.constants.CacheKeyConstants;
import com.core.zyyz.msdp.SpringContext;
import com.core.zyyz.redis.temp.InitializerCache;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.util.Assert;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class DictUtils {

    private static Map<String, List<BaseDictionary>> dictMap = Maps.newConcurrentMap();

    private DictUtils() {
    }

    public static List<BaseDictionary> get(String appCode, String dictType) {
        // Assert 断言
        Assert.notNull(appCode, "appCode can not be null or empty");
        Assert.notNull(dictType, "dictType can not be null or empty");
        String key = CacheKeyConstants.generateDictKey(appCode + "_" + dictType);
        return getFromDictMapOrRedis(key);
    }

    private static List<BaseDictionary> getFromDictMapOrRedis(String key) {
        List<BaseDictionary> dictList = (List)dictMap.get(key);
        if (null != dictList) {
            return dictList;
        } else {
            dictList = getList(key);
            if (null != dictList) {
                dictMap.put(key, dictList);
            }

            return dictList;
        }
    }

    private static List<BaseDictionary> getList(String key) {
        InitializerCache cache = (InitializerCache) SpringContext.getBean(InitializerCache.class);
        return cache.getList(key, BaseDictionary.class);
    }

    public static void refreshDictMap(String key) {
        dictMap.computeIfPresent(key, (k, v) -> {
            return getList(key);
        });
    }

    public static BaseDictionary get(String appCode, String dictType, String dictCode) {
        List<BaseDictionary> list = get(appCode, dictType);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        } else {
            Iterator var4 = list.iterator();

            BaseDictionary dict;
            do {
                if (!var4.hasNext()) {
                    return null;
                }

                dict = (BaseDictionary)var4.next();
            } while(!dict.getValue().equals(dictCode));

            return dict;
        }
    }

    public static List<BaseDictionary> get(String appCode, String dictType, List<String> includes, List<String> excludes) {
        List<BaseDictionary> dictList = get(appCode, dictType);
        return get(dictList, includes, excludes);
    }

    private static List<BaseDictionary> get(List<BaseDictionary> dictList, List<String> includes, List<String> excludes) {
        if (CollectionUtils.isEmpty(dictList)) {
            return Lists.newArrayList();
        } else {
//            HashSet excludeSet;
            if (CollectionUtils.isNotEmpty(includes)) {
                HashSet excludeSet = Sets.newHashSet(includes);
                dictList = (List)dictList.stream().filter((dict) -> {
                    return excludeSet.contains(dict.getValue());
                }).collect(Collectors.toList());
            }

            if (CollectionUtils.isNotEmpty(excludes)) {
                HashSet excludeSet = Sets.newHashSet(excludes);
                dictList = (List)dictList.stream().filter((dict) -> {
                    return !excludeSet.contains(dict.getValue());
                }).collect(Collectors.toList());
            }

            return dictList;
        }
    }

    public static void clearDictMap() {
        dictMap = Maps.newConcurrentMap();
    }
}
